/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.dspl.model;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.dspl.model.DatasetItems.immutableCopyOf;

import com.google.common.base.Objects;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * Textual information about an element of the dataset.
 *
 * @author Shardul Deo
 */
public final class Info implements DatasetItem<Info> {

  private static final long serialVersionUID = -3188539543404404119L;

  private Map<String, String> names = Maps.newHashMap();
  private Map<String, String> descriptions = Maps.newHashMap();
  private Map<String, String> urls = Maps.newHashMap();

  /**
   * @return Name for the given language
   */
  public String getName(String language) {
    return names.get(language);
  }

  /**
   * Sets name for the given language
   */
  public void setName(String language, String name) {
    names.put(language, name);
  }

  /**
   * @return Map of language to localized names
   */
  public Map<String, String> getNames() {
    return immutableCopyOf(names);
  }

  /**
   * Sets map of language to localized names
   */
  public void setNames(Map<String, String> names) {
    this.names.clear();
    this.names.putAll(checkNotNull(names));
  }

  /**
   * @return Description for the given language
   */
  public String getDescription(String language) {
    return descriptions.get(language);
  }

  /**
   * Sets description for the given language
   */
  public void setDescription(String language, String description) {
    descriptions.put(language, description);
  }

  /**
   * @return Map of language to localized descriptions
   */
  public Map<String, String> getDescriptions() {
    return immutableCopyOf(descriptions);
  }

  /**
   * Sets map of language to localized descriptions
   */
  public void setDescriptions(Map<String, String> descriptions) {
    this.descriptions.clear();
    this.descriptions.putAll(checkNotNull(descriptions));
  }

  /**
   * @return Url for the given language
   */
  public String getUrl(String language) {
    return urls.get(language);
  }

  /**
   * Sets url for the given language
   */
  public void setUrl(String language, String url) {
    urls.put(language, url);
  }

  /**
   * @return Map of language to localized urls
   */
  public Map<String, String> getUrls() {
    return immutableCopyOf(urls);
  }

  /**
   * Sets map of language to localized urls
   */
  public void setUrls(Map<String, String> urls) {
    this.urls.putAll(checkNotNull(urls));
  }

  @Override
  public Info clone() {
    Info info = new Info();
    info.setDescriptions(descriptions);
    info.setNames(names);
    info.setUrls(urls);

    checkState(equals(info));
    return info;
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(names, descriptions, urls);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }

    if (!(obj instanceof Info)) {
      return false;
    }

    Info other = (Info) obj;
    return Objects.equal(names, other.names)
        && Objects.equal(descriptions, other.descriptions)
        && Objects.equal(urls, other.urls);
  }

  @Override
  public String toString() {
    return Objects.toStringHelper(this)
        .add("names", names)
        .toString();
  }
}
